layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 订单重量复核管理
     */
    var OrderWeightCheck = {
        tableId: "orderWeightCheckTable"
    };

    /**
     * 初始化表格的列
     */
    OrderWeightCheck.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键ID'},
            {field: 'orderNo', sort: true, title: '订单号'},
            {field: 'weight', sort: true, title: '订单重量(克)'},
            {field: 'checkWeight', sort: true, title: '复核重量(克)'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'updateTime', sort: true, title: '修改时间'},
            {fixed: 'right',width: 120,align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    OrderWeightCheck.search = function () {
        var queryData = {};
        queryData['orderNo'] = $("#orderNo").val();
        table.reload(OrderWeightCheck.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    OrderWeightCheck.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/orderWeightCheck/add';
    };

    /**
     * 导出excel按钮
     */
    OrderWeightCheck.exportExcel = function () {
        var checkRows = table.checkStatus(OrderWeightCheck.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    OrderWeightCheck.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/orderWeightCheck/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    OrderWeightCheck.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/orderWeightCheck/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(OrderWeightCheck.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + OrderWeightCheck.tableId,
        url: Feng.ctxPath + '/orderWeightCheck/list',
        page: true,
        limit: 1000,
        limits:[1000,1500,2000,2500],
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: OrderWeightCheck.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        OrderWeightCheck.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        OrderWeightCheck.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        OrderWeightCheck.exportExcel();
    });


    // 导入
    $('#btnExpimport').click(function () {
        layer.open({
            type: 2,
            title: '订单重量导入',
            area: ['1000px', '750px'],
            anim: 5,
            content: Feng.ctxPath + '/orderWeightCheck/toimportExcel',
            end: function () {
                table.reload(OrderWeightCheck.tableId);
            }
        });
    });

    // 工具条点击事件
    table.on('tool(' + OrderWeightCheck.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            OrderWeightCheck.openEditDlg(data);
        } else if (layEvent === 'delete') {
            OrderWeightCheck.onDeleteItem(data);
        }
    });
});
